﻿using System;
using System.ComponentModel;
using Microsoft.Practices.Unity;
using System.Configuration;

namespace SmartFront.Toolkit.Core
{
    /// <summary>
    /// Default Container for all objects created by Unity.
    /// </summary>
    public static class UnityWrapper
    {
        /// <summary>
        /// Initializes the <see cref="Unity"/> class.
        /// </summary>
        static UnityWrapper()
        {
            Default = new UnityContainer();
        }

        /// <summary>
        /// Gets the default container consumers of unity should use.
        /// </summary>
        /// <value>The default container.</value>
        public static IUnityContainer Default { get; private set; }

        /// <summary>
        /// Resolves this instance.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "not our design, mimicing unity")]
        public static T Resolve<T>()
        {
            return Default.Resolve<T>();
        }

        /// <summary>
        /// Resolves the instance with the specified name.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "not our design, mimicing unity")]
        public static T Resolve<T>(string name)
        {
            return Default.Resolve<T>(name);
        }

        /// <summary>
        /// Resolves the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static object Resolve(Type type)
        {
            return Default.Resolve(type);
        }

        /// <summary>
        /// Resolves the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static object Resolve(Type type, string name)
        {
            return Default.Resolve(type, name);
        }

        /// <summary>
        /// Registers the instance.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance">The instance.</param>
        /// <param name="lifetimeManager">The lifetime manager.</param>
        internal static void RegisterInstance<T>(T instance, SelfControlledLifetimeManager lifetimeManager)
        {
            Default.RegisterInstance<T>(instance, lifetimeManager);
        }
    }
}
